package com.hmily.cloud.asm;

import com.hmily.cloud.asm.core.GenerateHandlerUtils;
import com.hmily.cloud.asm.core.Handler;
import com.hmily.cloud.asm.core.HexUtils;
import com.hmily.cloud.asm.core.UserBean;

/**
 * <h1>Asm测试类。</h1>
 *
 * @author hmilyylimh
 *         ^_^
 * @version 0.0.1
 *         ^_^
 * @date 2024/6/1
 *
 */
public class AsmLoadTest {

    /**
     * <h2>加载字节码，并调用。</h2>
     *
     * @param: args
     * @return: void
     */
    public static void main(String[] args) throws Exception {
        // 字节码
        String hexStr = "CAFEBABE 00000034 00320100 28636F6D 2F686D69 6C792F63 6C6F7564 2F61736D 2F636F72 652F5573 65724265 616E4861 6E646C65 72070001 0100594C 6A617661 2F6C616E 672F4F62 6A656374 3B4C636F 6D2F686D 696C792F 636C6F75 642F6173 6D2F636F 72652F48 616E646C 65723C4C 636F6D2F 686D696C 792F636C 6F75642F 61736D2F 636F7265 2F557365 72426561 6E3B3E3B 0100106A 6176612F 6C616E67 2F4F626A 65637407 00040100 20636F6D 2F686D69 6C792F63 6C6F7564 2F61736D 2F636F72 652F4861 6E646C65 72070006 01001455 73657242 65616E48 616E646C 65722E6A 61766101 00063C69 6E69743E 01000328 29560C00 09000A0A 0005000B 01000961 64644265 666F7265 01002628 4C636F6D 2F686D69 6C792F63 6C6F7564 2F61736D 2F636F72 652F5573 65724265 616E3B29 56010021 636F6D2F 686D696C 792F636C 6F75642F 61736D2F 636F7265 2F557365 72426561 6E07000F 01000767 65744E61 6D650100 1428294C 6A617661 2F6C616E 672F5374 72696E67 3B0C0011 00120A00 10001301 00106A61 76612F6C 616E672F 53747269 6E670700 15010006 6C656E67 74680100 03282949 0C001700 180A0016 00190100 176A6176 612F6C61 6E672F53 7472696E 67427569 6C646572 07001B0A 001C000B 01000823 4245464F 52452308 001E0100 06617070 656E6401 002D284C 6A617661 2F6C616E 672F5374 72696E67 3B294C6A 6176612F 6C616E67 2F537472 696E6742 75696C64 65723B0C 00200021 0A001C00 22010008 746F5374 72696E67 0C002400 120A001C 00250100 07736574 4E616D65 01001528 4C6A6176 612F6C61 6E672F53 7472696E 673B2956 0C002700 280A0010 00290100 15284C6A 6176612F 6C616E67 2F4F626A 6563743B 29560C00 0D000E0A 0002002C 01000443 6F646501 000D5374 61636B4D 61705461 626C6501 00095369 676E6174 75726501 000A536F 75726365 46696C65 00210002 00050001 00070000 00030001 0009000A 0001002E 00000011 00010001 00000005 2AB7000C B1000000 00000100 0D000E00 01002E00 00004100 03000200 00002C2B B60014C6 00272BB6 0014B600 1A9E001D 2BBB001C 59B7001D 121FB600 232BB600 14B60023 B60026B6 002AB100 00000100 2F000000 0300012B 1041000D 002B0001 002E0000 00150002 00020000 00092A2B C00010B6 002DB100 00000000 02003000 00000200 03003100 00000200 08";
        hexStr = hexStr.replaceAll(" ", "");
        byte[] classBytes = HexUtils.convertHexStringToBytes(hexStr);

        Class respObject = GenerateHandlerUtils.loadClass(
                "com.hmily.cloud.asm.core.UserBeanHandler",
                classBytes);
        Handler newHandler = (Handler)respObject.newInstance();

        // 执行调用
        UserBean userBean = new UserBean("geek");
        newHandler.addBefore(userBean);
        System.out.println(userBean);
    }
}